package com.example.androidtest;

import java.util.Random;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class InGameFragment extends Fragment implements OnClickListener {

	private ImageView image;
	private TextView words;
	private TextView hint;
	private Button[] buttons = new Button[26];
	private String word;
	private StringBuilder text;
	private int misstakes;
	private int correct;
	private int wordLenght;
	private Button newGame;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		misstakes = 0;
		correct = 0;
		wordLenght = 0;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.in_game_activity, container,
				false);

		image = (ImageView) view.findViewById(R.id.image);
		image.setImageResource(R.drawable.p0);
		words = (TextView) view.findViewById(R.id.words);
		hint = (TextView) view.findViewById(R.id.hint);

		newWord();

		newGame = (Button) view.findViewById(R.id.new_game);
		newGame.setOnClickListener(this);

		buttons[0] = (Button) view.findViewById(R.id.a);
		buttons[1] = (Button) view.findViewById(R.id.b);
		buttons[2] = (Button) view.findViewById(R.id.c);
		buttons[3] = (Button) view.findViewById(R.id.d);
		buttons[4] = (Button) view.findViewById(R.id.e);
		buttons[5] = (Button) view.findViewById(R.id.f);
		buttons[6] = (Button) view.findViewById(R.id.g);
		buttons[7] = (Button) view.findViewById(R.id.h);
		buttons[8] = (Button) view.findViewById(R.id.i);
		buttons[9] = (Button) view.findViewById(R.id.j);
		buttons[10] = (Button) view.findViewById(R.id.k);
		buttons[11] = (Button) view.findViewById(R.id.l);
		buttons[12] = (Button) view.findViewById(R.id.m);
		buttons[13] = (Button) view.findViewById(R.id.n);
		buttons[14] = (Button) view.findViewById(R.id.o);
		buttons[15] = (Button) view.findViewById(R.id.p);
		buttons[16] = (Button) view.findViewById(R.id.q);
		buttons[17] = (Button) view.findViewById(R.id.r);
		buttons[18] = (Button) view.findViewById(R.id.s);
		buttons[19] = (Button) view.findViewById(R.id.t);
		buttons[20] = (Button) view.findViewById(R.id.u);
		buttons[21] = (Button) view.findViewById(R.id.v);
		buttons[22] = (Button) view.findViewById(R.id.w);
		buttons[23] = (Button) view.findViewById(R.id.x);
		buttons[24] = (Button) view.findViewById(R.id.y);
		buttons[25] = (Button) view.findViewById(R.id.z);

		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setOnClickListener(this);
		}

		return view;
	}

	private void newWord() {
		String[] countries = getResources().getStringArray(
				R.array.countries_array);
		Random rand = new Random();
		word = countries[rand.nextInt(countries.length)];

		text = new StringBuilder();
		for (int i = 0; i < word.length(); i++) {
			if (word.charAt(i) == ' ') {
				text.append("  ");
				continue;
			}
			wordLenght++;
			text.append("_ ");
		}
		words.setText(text.toString().trim());
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.a:
			checkLetter('a', buttons[0]);
			break;
		case R.id.b:
			checkLetter('b', buttons[1]);
			break;
		case R.id.c:
			checkLetter('c', buttons[2]);
			break;
		case R.id.d:
			checkLetter('d', buttons[3]);
			break;
		case R.id.e:
			checkLetter('e', buttons[4]);
			break;
		case R.id.f:
			checkLetter('f', buttons[5]);
			break;
		case R.id.g:
			checkLetter('g', buttons[6]);
			break;
		case R.id.h:
			checkLetter('h', buttons[7]);
			break;
		case R.id.i:
			checkLetter('i', buttons[8]);
			break;
		case R.id.j:
			checkLetter('j', buttons[9]);
			break;
		case R.id.k:
			checkLetter('k', buttons[10]);
			break;
		case R.id.l:
			checkLetter('l', buttons[11]);
			break;
		case R.id.m:
			checkLetter('m', buttons[12]);
			break;
		case R.id.n:
			checkLetter('n', buttons[13]);
			break;
		case R.id.o:
			checkLetter('o', buttons[14]);
			break;
		case R.id.p:
			checkLetter('p', buttons[15]);
			break;
		case R.id.q:
			checkLetter('q', buttons[16]);
			break;
		case R.id.r:
			checkLetter('r', buttons[17]);
			break;
		case R.id.s:
			checkLetter('s', buttons[18]);
			break;
		case R.id.t:
			checkLetter('t', buttons[19]);
			break;
		case R.id.u:
			checkLetter('u', buttons[20]);
			break;
		case R.id.v:
			checkLetter('v', buttons[21]);
			break;
		case R.id.w:
			checkLetter('w', buttons[22]);
			break;
		case R.id.x:
			checkLetter('x', buttons[23]);
			break;
		case R.id.y:
			checkLetter('y', buttons[24]);
			break;
		case R.id.z:
			checkLetter('z', buttons[25]);
			break;
		case R.id.new_game:
			newGame();
			break;
		}
	}

	private void newGame() {
		image.setImageResource(R.drawable.p0);
		misstakes = 0;
		correct = 0;
		wordLenght = 0;
		newWord();
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setEnabled(true);
		}
		newGame.setVisibility(View.GONE);
		hint.setVisibility(View.VISIBLE);
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setBackgroundResource(R.drawable.empty);
		}
	}

	private void checkLetter(char letter, Button button) {
		button.setEnabled(false);
		if (word.contains(String.valueOf(letter))) {
			showLetters(letter, button);
		} else {
			if (misstakes < 6) {
				button.setBackgroundResource(R.drawable.wrong);
				misstakes++;
				changeImage(misstakes);
			} else {
				gameOver();
			}
		}
	}

	private void showLetters(char letter, Button button) {
		for (int i = 0; i < word.length(); i++) {
			if (word.charAt(i) == letter) {
				String temp = String.valueOf(letter);
				if (word.indexOf(temp, i) == 0 || word.charAt(i - 1) == ' ') {
					text.replace(word.indexOf(temp, i) * 2,
							word.indexOf(temp, i) * 2 + 1, temp.toUpperCase());
				} else {
					text.replace(word.indexOf(temp, i) * 2,
							word.indexOf(temp, i) * 2 + 1, temp);
				}
				correct++;
				if (correct == wordLenght) {
					image.setImageResource(R.drawable.win);
					hint.setVisibility(View.GONE);
					newGame.setVisibility(View.VISIBLE);
					disableKeyBoard();
				}
			}
		}
		button.setBackgroundResource(R.drawable.right);
		words.setText(text.toString());
	}

	private void gameOver() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < word.length(); i++) {
			if (i == 0) {
				String temp = String.valueOf(word.charAt(0)).toUpperCase();
				sb.append(temp + " ");
			} else {
				sb.append(word.charAt(i) + " ");
			}
		}
		words.setText(sb.toString());
		image.setImageResource(R.drawable.game_over);
		hint.setVisibility(View.GONE);
		newGame.setVisibility(View.VISIBLE);
		disableKeyBoard();
	}

	private void disableKeyBoard() {
		for (int i = 0; i < buttons.length; i++) {
			buttons[i].setEnabled(false);
		}
	}

	private void changeImage(int mistakes) {
		switch (mistakes) {
		case 1:
			image.setImageResource(R.drawable.p1);
			break;
		case 2:
			image.setImageResource(R.drawable.p2);
			break;
		case 3:
			image.setImageResource(R.drawable.p3);
			break;
		case 4:
			image.setImageResource(R.drawable.p4);
			break;
		case 5:
			image.setImageResource(R.drawable.p5);
			break;
		case 6:
			image.setImageResource(R.drawable.p6);
			break;
		}
	}
}
